#include "uploaddialog.h"
#include "ui_uploaddialog.h"
#include "debug.h"

UploadDialog::UploadDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UploadDialog)
{
    ui->setupUi(this);
    init();
}

UploadDialog::~UploadDialog()
{
    delete ui;
}

void UploadDialog::init()
{
    initStyleSheet();
    initPanel();
}

void UploadDialog::initStyleSheet()
{
    ui->panel->setStyleSheet(R"(
    #panel {
        background-color: rgb(247, 249, 252);
        border-radius: 12px;
    })");
    ui->head->setStyleSheet(R"(
    #head {
        background-color: transparent;
    })");
    ui->btn_close->setStyleSheet(R"(QPushButton { color: #787F8D; background-color: qlineargradient(x1:0, y1:0, x2:1, y2:1,stop:0 #f8f1f8, stop:1 #fbe6ee);border: none;border-radius: 12px;padding: 12px;}QPushButton:hover {background-color: qlineargradient(x1:0, y1:0, x2:1, y2:1,stop:0 #fcdfea, stop:1 #f8c7e1);})");
    ui->btn_upload->setStyleSheet(R"(border-radius: 12px; background-color: #FC3D4B; color: #ffffff;)");

    ui->title->setStyleSheet(R"(background-color: #DCDFE4; border-radius: 12px; color: #333333; text-align: left; padding-left: 20px;)");
    ui->content->setStyleSheet(R"(background-color: #DCDFE4; color: #333333;)");
}

#include <QGraphicsDropShadowEffect>
void UploadDialog::initPanel()
{
    ui->panel->setFixedSize(1200, 900);

    // 顶层(MainWidget)：无边框 + 透明，仅做容器
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    // 给内容面板(panel)加阴影
    auto *effect = new QGraphicsDropShadowEffect(ui->panel);
    effect->setColor(Qt::black);
    effect->setOffset(0, 0); // 0偏移
    effect->setBlurRadius(6); // 模糊半径
    ui->panel->setGraphicsEffect(effect);

    ui->panel->layout()->setSpacing(0);
    ui->panel->layout()->setContentsMargins(0, 10, 0, 10);

    ui->head->layout()->setSpacing(20);
    ui->head->layout()->setContentsMargins (20, 0, 20, 0);
    ui->head->setFixedHeight(60);

    ui->btn_close->setFixedSize(110, 50);

    ui->body->layout()->setSpacing(10);
    ui->body->layout()->setContentsMargins (20, 10, 20, 10);

    ui->title->setFixedHeight(50);
    ui->title->setPlaceholderText("请输入文章标题(5 ~ 100字)");
#if TEST_UI
    ui->title->setText("这是测试标题");
#endif
    ui->utils->layout()->setSpacing(0);
    ui->utils->layout()->setContentsMargins(20, 0, 20, 0);
    ui->utils->setFixedHeight(60);

    ui->btn_upload->setFixedSize(110, 50);
}

void UploadDialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        _dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
        _isDraging = true;
        event->accept();
    }
}

void UploadDialog::mouseMoveEvent(QMouseEvent *event)
{
    if ((event->buttons() & Qt::LeftButton) && _isDraging) {
        move(event->globalPosition().toPoint() - _dragPosition);
        event->accept();
    }
}

void UploadDialog::mouseReleaseEvent(QMouseEvent *event)
{
    (void)event;
    _isDraging = false;
}

void UploadDialog::on_btn_close_clicked()
{
    UploadDialog::reject();
}

void UploadDialog::on_btn_upload_clicked()
{
    auto titleSize = ui->title->text().size(), contentSize = ui->content->toPlainText().size();
    if(titleSize < 5 || titleSize > 100 || contentSize > 2000) return; // TODO: 加错误弹窗,进行提示
    UploadDialog::accept();
}
